package com.recruit.servlet;

import com.recruit.entity.CompanyUser;
import com.recruit.entity.User;
import com.recruit.service.CompanyLoginAndRegistService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/****
 * author:丁文彬
 * time：2020.9.26
 */
@WebServlet(name = "CompanyUserLoginAndRegistServlet",urlPatterns = "/company")
public class CompanyUserLoginAndRegistServlet extends HttpServlet {
    //service层
    private CompanyLoginAndRegistService companyLoginAndRegistService = new CompanyLoginAndRegistService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取操作方式
        String method = request.getParameter("method");
        //登录
        if ("login".equals(method)){
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            CompanyUser companyUser = companyLoginAndRegistService.companyUserLogin(username, password);
            if (companyUser!=null){
                //传送user对象到前端
                request.setAttribute("companyUser",companyUser);
                //主页
                request.getRequestDispatcher("companyMain.jsp").forward(request,response);
            }else {
                request.setAttribute("companyUserLoginMsg","用户名和密码错误!");
                request.getRequestDispatcher("companyUser.jsp").forward(request,response);
            }
        }else if ("register".equals(method)) {
            //获取注册数据
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String email = request.getParameter("email");
            String phone = request.getParameter("phone");
            String companyName = request.getParameter("companyName");

            //CompanyUser
            CompanyUser companyUser = new CompanyUser();
            companyUser.setUsername(username);
            companyUser.setPassword(password);
            companyUser.setEmail(email);
            companyUser.setPhone(phone);
            companyUser.setCompanyName(companyName);
            System.out.println(companyName);
            //获取service层注册服务
            int index = companyLoginAndRegistService.companyUserRegist(companyUser);
            if (index>0){
                request.setAttribute("companyUserRegisterMsg","注册成功，请登录!");
                request.getRequestDispatcher("companyUser.jsp").forward(request,response);
            }else {
                request.setAttribute("companyUserRegisterMsg","注册失败，请联系管理员!");
                request.getRequestDispatcher("companyUser.jsp").forward(request,response);
            }
        }
    }
}
